package com.cy.pj.modules.controller;

import com.alibaba.excel.EasyExcel;
import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.web.domain.BaseController;
import com.cy.pj.framework.excel.util.ExcelUtil;
import com.cy.pj.modules.entity.Sku;
import com.cy.pj.modules.service.ISkuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * 商品货品Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/sku")
public class SkuController extends BaseController
{
    private String prefix = "modules/sku";

    @Autowired
    private ISkuService skuService;

    @RequiresPermissions("sku:view")
    @GetMapping()
    public String sku()
    {
        return prefix + "/sku";
    }

    /**
     * 查询商品货品列表
     */
    @RequiresPermissions("sku:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(Sku sku,Integer pageCurrent)
    {
        return new JsonResult(skuService.selectSkuList(sku,pageCurrent));
    }

    /**
     * 导入：同步读，单sheet
     * 注意：这里为了介绍 excel导入导出，代码都写在 controller，实际项目开发中，校验和处理代码建议放到 service
     */
    @PostMapping("/excel/import")
    public JsonResult importData(MultipartFile file) throws ParseException {
        // 1.excel同步读取数据
        List<Sku> skuExcelList = new ArrayList<>();
        try {
           skuExcelList = EasyExcel.read(new BufferedInputStream(file.getInputStream())).head(Sku.class).sheet().doReadSync();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msg = skuService.importDataSku(skuExcelList);
        return new JsonResult(msg);
    }
    /**
     * 导出
     */
    @RequiresPermissions("sku:export")
    @GetMapping("/excel/export")
    public void exportData(HttpServletResponse response,Sku sku, Integer pageCurrent) {
        String fileName = "商品列表";
        String sheetName = "商品列表";
        List<Sku> skuList = skuService.selectSkuList(sku,pageCurrent).getRecords();
        try {
            ExcelUtil.writeExcel(response, skuList, fileName, sheetName, Sku.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 下载Excel模板
     */
    @GetMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入商品模板";
        String sheetName = "导入商品模板";
        List<Sku> skuList = new ArrayList<>();
        try {
            ExcelUtil.writeExcel(response, skuList, fileName, sheetName, Sku.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增商品货品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品货品
     */
    @RequiresPermissions("sku:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(@Validated Sku sku)
    {
        return new JsonResult(skuService.insertSku(sku));
    }

    /**
     * 修改商品货品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap, HttpServletRequest request)
    {
        Sku sku = skuService.selectSkuById(id);
        mmap.put("sku", sku);
        if(sku!=null){
            mmap.put("spuId",sku.getSpuId());
            request.setAttribute("spuId",sku.getSpuId());
        }
        return prefix + "/sku_edit";
    }

    @GetMapping("/id")
    @ResponseBody
    public JsonResult getById(Long id){
        return new JsonResult(skuService.selectSkuById(id));
    }

    @GetMapping("/idSpuName")
    @ResponseBody
    public JsonResult selectSkuSpuNameById(Long id){
        return new JsonResult(skuService.selectSkuAndSpuNameById(id));
    }

    /**
     * 修改保存商品货品
     */
    @RequiresPermissions("sku:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(@Validated Sku sku)
    {
        return new JsonResult(skuService.updateSku(sku));
    }

    /**
     * 删除商品货品
     */
    @RequiresPermissions("sku:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {
        return new JsonResult(skuService.deleteSkuByIds(ids));
    }
}
